<!--
 * @Author: wuheng wuheng@szhbzc.com
 * @Date: 2024-12-24 17:35:34
 * @LastEditors: wuheng wuheng@szhbzc.com
 * @LastEditTime: 2025-02-19 10:35:36
 * @FilePath: \web-appc:\Users\wuheng\Desktop\WugangNew\src\views\dishmana\dishaccounting\components\consuminfo.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="detailTable">
    <el-table :data="ConsumablesData" max-height="500" show-summary :summary-method="getSummaries">
      <el-table-column type="index" label="序号" width="120" />
      <el-table-column prop="skuName" label="名称" width="180" />
      <el-table-column prop="minimumUnitName" label="最小单位" />
      <el-table-column prop="price" label="价格(元)" :align="'right'" />
      <el-table-column prop="specifications" label="规格" />
      <el-table-column prop="amount" label="数量">
        <template #default="{ row }">
          <div>
            <pb-input v-model.number="row.amount" type="number" width="180px" placeholder="请輸入數量" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="cost" label="成本(元)" :align="'right'" />
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { GetDishBomConsumablesApi } from '@/api/Dashmana'
const props = defineProps({
  Bomdata: {
    type: Object,
    default: {}
  }
})
const getSummaries = ({ columns, data }: any) => {
  const sums = [] as any;
  columns.forEach((column: any, index: any) => {
    if (index === 0) {
      // 第一列是ID，不显示总计
      sums[index] = '总计';
      return;
    }
    // console.log('1', column.property)
    if (column.property === 'cost') {
      // console.log('2', column.property)
      // 只为amount1和amount2计算总计
      const values = data.map(item => Number(item[column.property] || 0));
      const sum = values.reduce((prev, curr) => {
        const value = Number(curr);
        if (!isNaN(value)) {
          return prev + curr;
        } else {
          return prev;
        }
      }, 0);

      sums[index] = sum;
    } else {
      // 其他列不显示总计
      sums[index] = '';
    }
  });

  return sums;
}
const getBomlistfn = () => {
  GetDishBomConsumablesApi({
    "currentPage": 1,
    "pageSize": -1,
    "sortingFields": [
      {
        "field": "",
        "order": ""
      }
    ],
    "dishId": props.Bomdata.dishId
  }).then((res: any) => {
    ConsumablesData.value = res.data ? res.data.records : []
  })
}
const getconsuinfoDatafn = () => {
  return ConsumablesData.value
}
const ConsumablesData = ref([
])
onMounted(() => {
  getBomlistfn()
})
defineExpose({ getconsuinfoDatafn });
</script>

<style lang="scss" scoped>
.detailTable {
  ::v-deep thead th {
    background: #F5F5F5 !important;
  }
}
</style>
